﻿using Iworld.ViewModel;
using System;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Backstage.Framework
{
    /// <summary>
    /// 后台的视图模型服务
    /// </summary>
    public class BackstageViewModelService : ViewModelServiceBase<Page,Pop>
    {
        private BackstageViewModelService(IMainPage<Page, Pop> root)
            : base(root)
        {}

        private static BackstageViewModelService current;

        /// <summary>
        /// 当前的后台视图模型服务
        /// </summary>
        public static BackstageViewModelService Current
        {
            get
            {
                if (current == null)
                {
                    throw new Exception("视图模型不存在");
                }
                return current;
            }
        }


        protected override void OnJumped(PageCreaterKey key)
        {
            base.OnJumped(key);
            if (key.IsDefault)
            {
               DataManager.RemoveValue(DataKey.Administrator_Info);
            }
        }


        public static void Initialize(IMainPage<Page, Pop> root)
        {
            if (current != null)
            {
                throw new Exception("已经初始化");
            }
            current = new BackstageViewModelService(root);
            current.JumpToDefaultPage();
        }

        protected override Assembly GetViewModelAssembly()
        {
            return Assembly.GetExecutingAssembly();
        }

        protected override string GetViewModelName(string pageTypeName)
        {
            return string.Format("Iworld.Backstage.Framework.{0}", new Regex("Page").Replace(pageTypeName, "ViewModel"));
        }
    }
}
